package com.itheima.reggie.service.Impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

//套餐
@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    //套餐分页查询
    @Override
    public Page<Setmeal> getPage(String name, Integer pageNum, Integer pageSize) {
        //通过name 模糊查询
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        //判断name 是否为空
        wrapper.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        //定义分页查询
        Page<Setmeal> page = new Page<>(pageNum, pageSize);
        page=setmealMapper.selectPage(page,wrapper);
        //得到分页所有的数据
        List<Setmeal> setmealList = page.getRecords();
        //遍历 数据 并通过id 设置名字
        if (CollectionUtil.isNotEmpty(setmealList)){
            for (Setmeal setmeal : setmealList) {
                //通过得到的某个setmeal 的id 查找对应的 category中 id 对应的 对象
                Category category = categoryMapper.selectById(setmeal.getCategoryId());
                //给对象设置名字
                setmeal.setCategoryName(category.getName());
                //根据setmeal的id从setmeal_dish表中查询列表
                LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(SetmealDish::getSetmealId,setmeal.getId());
                List<SetmealDish> setmealDishes = setmealDishMapper.selectList(wrapper1);
                setmeal.setSetmealDishes(setmealDishes);
            }
        }
        return page;

    }
    //套餐保存方法
    @Override
    public void save(Setmeal setmeal) {
        //1. 保存基本信息到setmeal表中
        setmealMapper.insert(setmeal);

        //2.获取setmeal中得setmealDishs ,遍历  保存到中间表
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishList)) {
            for (SetmealDish setmealDish : setmealDishList) {
                setmealDish.setSetmealId(setmeal.getId());//关联上套餐id
                setmealDishMapper.insert(setmealDish);//执行保存
            }
        }
    }
    //删除方法
    @Override
    public void deleteByIds(Long[] ids) {
            //1. 判断删除的套餐中是否有还在销售,如果有直接报错
            LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
            wrapper.in(Setmeal::getId, ids);//where id in #{ids}
            wrapper.eq(Setmeal::getStatus, 1);//and status = 1
            int count = setmealMapper.selectCount(wrapper);
            if (count > 0) {
                throw new CustomException("您要删除的套餐中有还在销售中,无法删除");
            }

            //2. 先删除套餐表中所有符合条件的数据
            //delete from setmeal where id in #{ids}
            setmealMapper.deleteBatchIds(Arrays.asList(ids));
            //3. 先删除套餐菜品中间表中所有符合条件的数据
            //delete from setmeal_dish where setmeal_id in #{id}
            LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.in(SetmealDish::getSetmealId, ids);
            setmealDishMapper.delete(wrapper1);
        }
        //设置禁用状态
    @Override
    public void setMyStatus(Integer status, Long[] ids) {
        for (Long id : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(status);
            setmealMapper.updateById(setmeal);
        }
    }
    //套餐根据id查找
    @Override
    public Setmeal findById(Long id) {
        //1. 查询套餐信息
        Setmeal setmeal = setmealMapper.selectById(id);

        //2. 根据setmeal中的category_id 从category表查询name
        Category category = categoryMapper.selectById(setmeal.getCategoryId());
        setmeal.setCategoryName(category.getName());

        //3. 根据setmeal的id从setmeal_dish表中查询列表
        LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(SetmealDish::getSetmealId, setmeal.getId());
        List<SetmealDish> setmealDisheList = setmealDishMapper.selectList(wrapper1);
        setmeal.setSetmealDishes(setmealDisheList);

        return setmeal;
    }
    //更改套餐
    @Override
    public void update(Setmeal setmeal) {
        //1. 根据主键更新菜品表信息
        setmealMapper.updateById(setmeal);

        //2. 根据套餐id删除中间表中相关菜品
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
        setmealDishMapper.delete(wrapper);

        //3. 向中间表保存新的菜品列表
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishList)) {
            for (SetmealDish setmealDish : setmealDishList) {
                setmealDish.setSetmealId(setmeal.getId());
                setmealDishMapper.insert(setmealDish);
            }
        }
    }
    //套餐前端展示
    @Override
    public List<Setmeal> findList(Long categoryId, Integer status) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId,categoryId);
        wrapper.eq(Setmeal::getStatus, status);
        List<Setmeal> setmealList = setmealMapper.selectList(wrapper);
        return setmealList;
    }
    //前端点击套餐图片显示所有c菜品
    @Override
    public List<SetmealDish> findMyDish(Long id) {
        //通过id查找套餐关联的菜品集合
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(wrapper);
        //遍历添加
        for (SetmealDish setmealDish : setmealDishes) {
            LambdaQueryWrapper<Dish> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(Dish::getId,setmealDish.getDishId());
            //查询所有id集合
            List<Dish> dishList = dishMapper.selectList(wrapper1);
            //遍历集合 将描述和照片设置其中
            for (Dish dish : dishList) {
                setmealDish.setDescription(dish.getDescription());
                setmealDish.setImage(dish.getImage());
            }

        }
        return setmealDishes;
    }
}

